package net.cathackers.devel.scmp.xmpp;

import org.dom4j.Document;
import org.dom4j.Element;

/**
 * MessagePackets are used to "push" information to another entity. Common uses
 * in instant messaging applications include single messages, messages sent in
 * the context of a chat conversation, messages sent in the context of a
 * multi-user chat room, headlines and other alerts, and errors.
 * 
 */
public class MessagePacket extends Packet {
    /**
     * Constructs a new MessagePacket by parsing incoming DOM object.
     * 
     * @param doc
     *            DOM object
     */
    public MessagePacket(Document doc) {
	super.setDocument(doc);
    }

    /**
     * Constructs a new MessagePacket
     */
    protected MessagePacket() {
	super();
	rootElement_ = codeBehind_.addElement("message");
    }

    /**
     * 
     * @return message type
     * @see MessageType
     */
    public MessageType getMessageType() {
	MessageType ret = MessageType.normal;
	String type = rootElement_.attributeValue("type");
	if (type != null) {
	    ret = MessageType.valueOf(type);
	}
	return ret;
    }

    /**
     * set message type
     * 
     * @see MessageType
     * @param type
     */
    public void setMessageType(MessageType type) {
	rootElement_.addAttribute("type", type == null ? "normal" : type
		.toString());
    }

    /**
     * @return the subject of this message
     */
    public String getSubject() {
	return rootElement_.elementText("subject");
    }

    /**
     * set the subject of this message
     * 
     * @param subject
     */
    public void setSubject(String subject) {
	Element subjectElement = rootElement_.element("subject");
	if (subjectElement == null) {
	    rootElement_.addElement("subject").setText(subject);
	} else {
	    subjectElement.setText(subject);
	}
    }

    /**
     * 
     * @return the body of this message "context"
     */
    public String getBody() {
	return rootElement_.elementText("body");
    }

    /**
     * set the body of this message
     * 
     * @param body
     *            the message context
     */
    public void setBody(String body) {
	Element bodyElement = rootElement_.element("body");
	if (bodyElement == null) {
	    rootElement_.addElement("body").setText(body);
	} else {
	    bodyElement.setText(body);
	}
    }

    /**
     * return an the value of thread attribute that is identifier that is used
     * for tracking a conversation thread ("instant messaging session") between
     * two entities.
     * 
     * @return the thread of this message
     */
    public String getThread() {
	return rootElement_.elementText("thread");
    }

    /**
     * set the thread attribute
     * 
     * @param thread
     */
    public void setThread(String thread) {
	Element threadElement = rootElement_.element("thread");
	if (threadElement == null) {
	    rootElement_.addElement("thread").setText(thread);
	} else {
	    threadElement.setText(thread);
	}
    }

    /**
     * represent the types of MessagePacket
     * 
     */
    public enum MessageType {
	/**
	 * The message is a single message that is sent outside the context of a
	 * one-to-one conversation enabling the recipient to reply, but without
	 * a conversation history like emails.
	 */
	normal,
	/**
	 * The message is sent in the context of a one-to-one chat conversation
	 */
	chat,
	/**
	 * The message is sent in the context of a multi-user chat environment
	 * similar to IRC.
	 */
	groupchat,
	/**
	 * The message is probably generated by an automated service that
	 * delivers or broadcasts content (news, sports, market information, RSS
	 * feeds, etc.)
	 */
	headline,
	/**
	 * An error has occurred related to a previous message sent by the
	 * sender.
	 */
	error;
	/**
	 * represent this message type as string
	 */
	@Override
	public String toString() {
	    return super.toString().toLowerCase();
	}

    }

}
